﻿using MESPro.DBManager;
using MESPro.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MESPro.DBManager
{
    public class FunctionGroupManager
    {
        #region 曾
        /// <summary>
        /// 添加一个功能组,不包括子类(add=httppost)
        /// </summary>
        /// <returns>返回插入成功的行数</returns>
        public int AddFunctionGroup(FunctionGroup FunctionGroup)
        {
            var res = DbContext.Db.Insertable(FunctionGroup).ExecuteCommand();
            return res;
        }
        /// <summary>
        /// 添加一个功能组,包括子类(add=httppost)
        /// </summary>
        /// <returns>返回插入成功的行数</returns>
        public bool AddFunctionGroupInfo(FunctionGroup FunctionGroup)
        {
            var res = DbContext.Db.InsertNav(FunctionGroup)
                .Include(x => x.Functions, new InsertNavOptions() { OneToManyIfExistsNoInsert = true })
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除一个功能组和关系表中的数据
        /// </summary>
        /// <returns>返回是否删除成功</returns>
        public bool DeleteFunctionGroup(FunctionGroup FunctionGroup)
        {
            List<FunctionGroup> list = new List<FunctionGroup> { FunctionGroup };
            var res = DbContext.Db
                .DeleteNav<FunctionGroup>(a => a.Id == FunctionGroup.Id)
                .Include(b => b.Functions, new DeleteNavOptions()
                {
                    ManyToManyIsDeleteA = true
                })
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 改
        /// <summary>
        /// 修改一个页面和他的子页面(Update==httpput).多对多
        /// 
        /// </summary>
        /// <returns></returns>
        public bool UpdateFunctionGroupInfo(FunctionGroup FunctionGroup)
        {
            List<FunctionGroup> list = new List<FunctionGroup>() { FunctionGroup };
            //var res = DbContext.Db
            //    .UpdateNav(list)
            //    .Include(z1 => z1.Functions, new UpdateNavOptions
            //    {
            //        ManyToManyIsUpdateA = true,
            //        ManyToManyIsUpdateB = true
            //    })
            //    .ExecuteCommand();//更新a 更新b 更新中间表
            var res = DbContext.Db.UpdateNav(list).Include(x => x.Functions).ExecuteCommand(); //只更新关系表
            return res;
        }
        #endregion

        #region 查
        /// <summary>
        /// 查询所有的页面,以及子类
        /// </summary>
        /// <returns></returns>
        public List<FunctionGroup> GetFunctionGroupListInfo()
        {
            var res = DbContext.Db
                .Queryable<FunctionGroup>()
                .Includes(
                b => b.Functions,
                c => c.DbControlGs)
                .ToList();
            return res;
        }
        /// <summary>
        /// 查询所有的页面,不包括子类
        /// </summary>
        /// <returns></returns>
        public List<FunctionGroup> GetFunctionGroupList()
        {
            var res = DbContext.Db.Queryable<FunctionGroup>().ToList();
            return res;
        }
        #endregion

    }
}
